package com.briup.jz.web.controller;

import com.briup.jz.bean.Product;
import com.briup.jz.bean.extend.ProductExtend;
import com.briup.jz.service.IProductService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 潘京博
 * @project: jz
 * @description:
 * @create: 2020/6/13 12:51
 */
@RestController
@RequestMapping("/product")
@Api(description = "产品管理接口")
public class ProductController {
    @Autowired
    private IProductService productService;

    /**
     * 查询所有产品，并且级联所有栏目
     * @return
     */

    @ApiOperation(value = "查找全部产品及其栏目")
    @GetMapping("findAllWithCategory")
    public Message findAllWithCategory(){
        List<ProductExtend> list = productService.findAllWithCategory();
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "通过id删除产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "唯一编号", required = true, paramType = "query")
    })
    @GetMapping("deleteById")
    public Message deleteById(long id){
        productService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "添加或更新产品，不传入id则为添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "不传入id则为添加", required = false, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", required = true, paramType = "query"),
            @ApiImplicitParam(name = "introduce", value = "介绍", required = true, paramType = "query"),
            @ApiImplicitParam(name = "price", value = "价格", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, paramType = "query"),
            @ApiImplicitParam(name = "photo", value = "不传入id则为添加", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sales", value = "", required = false, paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "所属栏目的id", required = true, paramType = "query")})
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Product product){
        productService.saveOrUpdate(product);
        return MessageUtil.success("操作成功");
    }

    @ApiOperation(value = "通过id查找产品及其栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "唯一编号", required = true, paramType = "query")
    })
    @GetMapping("findByIdWithCategory")
    public Message findByIdWithCategory(long id){
        return MessageUtil.success("查找成功", productService.findByIdWithCategory(id));
    }
}
